#define _CRT_SECURE_NO_WARNINGS 1

// qsort的练习使用

// 1. 整形排序(降序)

#include <stdio.h>
#include <stdlib.h>
void print(int arr[], int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		printf("%d ", arr[i]);
	}
}
int cmp(const void* p1, const void* p2)
{
	return *(int*)p2 - *(int*)p1;
}

int main()
{
	int arr[10] = { 0,1,2,3,4,5,6,7,8,9 };
	int num = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, num, sizeof(int), cmp);
	print(arr, num);
	return 0;
}

 // 2. 字符串排序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print(char arr[3][10], int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		printf("%s ", arr[i]);
	}
}
int cmp(const void* p1, const void* p2)
{
	return strcmp((char*)p1, (char*)p2) ;
}

int main()
{
	char arr[3][10] = { "lisi","zhangsan",  "wangwu"};
	int num = sizeof(arr)/sizeof(arr[0]);
	qsort(arr, num, sizeof(arr[0]), cmp);
	print(arr, num);
	return 0;
}

// 3.1 结构体排序（by姓名）

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
	int age;
	char name[20];
};
void print(struct student stu[], int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		printf("%s ", stu[i].name);
	}
}
int cmp(const void* p1, const void* p2)
{
	return strcmp(((struct student*)p1)->name, ((struct student*)p2)->name);
}

int main()
{
	struct student stu[3] = { {20, "zhangsan"}, {30, "lisi"}, {40, "wangwu"} };
	int num = sizeof(stu) / sizeof(stu[0]);
	qsort(stu, num, sizeof(stu[0]), cmp);
	print(stu, num);
	return 0;
}

 // 3.2 结构体排序（by年龄）

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
	int age;
	char name[20];
};
void print(struct student stu[], int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		printf("%d ", stu[i].age);
	}
}
int cmp(const void* p1, const void* p2)
{
	return ((struct student*)p1)->age - ((struct student*)p2)->age;
}

int main()
{
	struct student stu[3] = { {20, "zhangsan"}, {30, "lisi"}, {40, "wangwu"} };
	int num = sizeof(stu) / sizeof(stu[0]);
	qsort(stu, num, sizeof(stu[0]), cmp);
	print(stu, num);
	return 0;
}


// 用冒泡排序实现qsort

#include <stdio.h>

void print(int arr[], int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		printf("%d ", arr[i]);
	}
}

int cmp(const void* p1, const void* p2)
{
	return *(int*)p2 - *(int*)p1;
}
void Swap(char* p1, char* p2, int size)
{
	int i = 0;
	for (i = 0; i < size; i++) // 一个字节一个字节进行交换
	{
		char temp = *p1;
		*p1 = *p2;
		*p2 = temp;
		p1++;
		p2++;
	}
}

bubble_qsort(void* base, int num, int size, int (*cmp)(const void* p1, const void* p2))
{
	int i = 0;
	for (i = 0; i < num -1 ; i++)
	{
		int j = 0;
		for (j = 0; j < num - i - 1; j++)
		{
			 // 判断
			if (cmp((char*)base + j * size, (char*)base + (j + 1) * size) > 0)
			{
				// 交换
				Swap((char*)base + j * size, (char*)base + (j + 1) * size, size);
			}
		}
	}
}

int main()
{
	int arr[10] = { 0,1,2,3,4,5,6,7,8,9 };
	int num = sizeof(arr) / sizeof(arr[0]);
	bubble_qsort(arr, num, sizeof(arr[0]), cmp);
	print(arr, num);
	return 0;
}

  // 3.2.1 结构体排序（by年龄）

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
	int age;
	char name[20];
};
void print(struct student stu[], int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		printf("%d ", stu[i].age);
	}
}
int cmp(const void* p1, const void* p2)
{
	return ((struct student*)p1)->age - ((struct student*)p2)->age;
}

void Swap(char* p1, char* p2, int size)
{
	int i = 0;
	for (i = 0; i < size; i++) // 一个字节一个字节进行交换
	{
		char temp = *p1;
		*p1 = *p2;
		*p2 = temp;
		p1++;
		p2++;
	}
}

bubble_qsort(void* base, int num, int size, int (*cmp)(const void* p1, const void* p2))
{
	int i = 0;
	for (i = 0; i < num - 1; i++)
	{
		int j = 0;
		for (j = 0; j < num - i - 1; j++)
		{
			// 判断
			if (cmp((char*)base + j * size, (char*)base + (j + 1) * size) > 0)
			{
				// 交换
				Swap((char*)base + j * size, (char*)base + (j + 1) * size, size);
			}
		}
	}
}
int main()
{
	struct student stu[3] = { {20, "zhangsan"}, {30, "lisi"}, {40, "wangwu"} };
	int num = sizeof(stu) / sizeof(stu[0]);
	qsort(stu, num, sizeof(stu[0]), cmp);
	print(stu, num);
	return 0;
}